﻿using System;

namespace NumbersToEnglish.Engine
{
    /// <summary>
    /// NumbersToEnglishBase base class
    /// </summary>
    /// <remarks>
    /// Centralise any exception handling
    /// </remarks>
    /// <author>Ferdeen Mughal</author>
    /// <version>$Id: NumbersToEnglishBase.cs,v 1.0 13/12/2013</version>
    public class NumbersToEnglishBase
    {
        protected T ExecuteFaultHandledOperation<T>(Func<T> codeToExecute)
        {
            try
            {
                return codeToExecute.Invoke();
            }
            catch (AggregateException e)
            {
                // task framework wraps the thrown exception within an aggregate exception
                throw new Exception(e.InnerException.Message);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        protected void ExecuteFaultHandledOperation(Action codeToExecute)
        {
            try
            {
                codeToExecute.Invoke();
            }
            catch (AggregateException e)
            {
                // task framework wraps the thrown exception within an aggregate exception
                throw new Exception(e.InnerException.Message);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
    }
}
